package com.bookheaven;

import java.util.List;
import java.util.Map;

import org.primefaces.model.SortOrder;

import com.bookheaven.beans.BookHeavenBean;
import com.bookheaven.core.entity.base.BaseEntity;

public class BaseLazyDataModel<T extends BaseEntity> extends org.primefaces.model.LazyDataModel<T> {
	
	private static final long		serialVersionUID	= -4138397119695033751L;
	
	private final BookHeavenBean<T>	bookHeavenBean;
	
	public BaseLazyDataModel(BookHeavenBean<T> bookHeavenBean) {
		this.bookHeavenBean = bookHeavenBean;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
		setWrappedData(bookHeavenBean.getEntityController().getPaginatedOrderedFilteredList(
				bookHeavenBean.getEntity().getClass(), first, first + pageSize, sortField, filters));
		return (List<T>) getWrappedData();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public T getRowData(String rowKey) {
		List<T> dataList = (List) getWrappedData();
		for (T data : dataList) {
			if (data.getId().equals(rowKey))
				return data;
		}
		
		return null;
	}
	
	// TODO ui tarafina id gidiyor belli ki
	@Override
	public Object getRowKey(T data) {
		return data.getId();
	}
}
